/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.iuh.dhth7dlt.emailclient.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.dom.Entity;
import org.apache.james.mime4j.dom.Message;
import org.apache.james.mime4j.dom.MessageBuilder;
import org.apache.james.mime4j.dom.TextBody;
import org.apache.james.mime4j.message.DefaultMessageBuilder;
import org.apache.james.mime4j.message.MultipartImpl;
import org.apache.james.mime4j.stream.Field;

public class Email {

    private int id;
    private int idEmail;
    private String senderFrom;
    private String senderTo;
    private String subject;
    private String cc;
    private String textBody;
    private String htmlBody;
    private Date date;
    private Message message;
    private MessageBuilder builder;

    public Email(int id,int idEmail, byte[] dataMail) throws IOException, MimeException, ParseException {
        builder = new DefaultMessageBuilder();
        message = builder.parseMessage(new ByteArrayInputStream(dataMail));
        parseBodyParts(message);
        this.id=id;
        this.idEmail=idEmail;
        Field getHeader = message.getHeader().getField("From");
        senderFrom = getHeader == null ? "" : StringHelper.convertFromUTF8(getHeader.toString().replace("From:", "").trim());
        getHeader = message.getHeader().getField("To");
        senderTo = getHeader == null ? "" : StringHelper.convertFromUTF8(getHeader.toString().replace("To:", "").trim());
        getHeader = message.getHeader().getField("CC");
        cc = getHeader == null ? "" : StringHelper.convertFromUTF8(getHeader.toString().replaceAll("CC:|Cc:", "").trim());
        getHeader = message.getHeader().getField("subject");
        subject = getHeader == null ? "" : StringHelper.convertFromUTF8(getHeader.toString().replace("Subject:", "").trim());
        getHeader = message.getHeader().getField("Date");
        String sendDate = getHeader == null ? "Wed, 4 Jul 2001 12:08:56 -0700" : getHeader.toString().replaceAll("Date:", "").trim();
        date = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH).parse(sendDate);
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
     public int getIdEmail() {
        return idEmail;
    }
    
    /**
     * @return the senderTo
     */
    public String getSenderTo() {
        return senderTo.replaceAll("<|>", "");
    }

    /**
     * @return the sender
     */
    public String getSenderFrom() {
        return senderFrom;
    }

    /**
     * @return the subject
     */
    public String getSubject() {
        return subject;
    }

    /**
     * @return the message
     */
    public String getTexttBody() {
        return textBody;
    }

    public String getHTMLBody() {
        return htmlBody;
    }

    public String getCC() {
        return cc.replaceAll("<|>", "");
    }

    /**
     * @return the time
     */
    public Date getDate() {
        return date;
    }

    private void parseBodyParts(Message message) throws IOException {
        if (message != null) {

            if (message.isMultipart()) {
                MultipartImpl b = (MultipartImpl) message.getBody();
                List<Entity> bodyParts = b.getBodyParts();

                for (Iterator<Entity> it = bodyParts.iterator(); it.hasNext();) {
                    Entity entity = it.next();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    TextBody text = null;
                    try {
                        text = (TextBody) entity.getBody();
                    } catch (ClassCastException e) {
                        return;
                    }
                    text.writeTo(baos);
                    switch (entity.getMimeType()) {
                        case "text/plain":
                            textBody = StringHelper.convertFromUTF8(new String(baos.toByteArray()));
                            break;
                        case "text/html":
                            htmlBody = new String(baos.toByteArray());
                            break;
                    }

                }
            } else {
                TextBody txt = (TextBody) message.getBody();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                txt.writeTo(baos);
                textBody = new String(baos.toByteArray());
                htmlBody = "Không có htmlBody";
            }

        }
    }
}
